/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package connexion;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Date;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author SA RA
 */
public class releveNote extends javax.swing.JFrame {
    public DefaultTableModel model;
    String cne = null;
    /**
     * Creates new form releveNote
     */
    public releveNote() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("CNE");

        jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources1/find.png"))); // NOI18N
        jToggleButton1.setText("Rechercher");
        jToggleButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jToggleButton1MouseClicked(evt);
            }
        });

        jLabel2.setText("Nom: ");

        jLabel3.setText("Prenom:");

        jLabel4.setText("Filière:");

        jLabel5.setText("Semestre:");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Module:", "Notes:"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Imprimer");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Modifier");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(111, 111, 111)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(jToggleButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(114, 114, 114)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(169, 169, 169)
                        .addComponent(jButton1)
                        .addGap(87, 87, 87)
                        .addComponent(jButton2)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButton1))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
    
                 Document doc = new Document();
            try {
                 connection connect = new connection();
            Statement st = connect.con.createStatement();
            
            String SQL = "SELECT * FROM etudiant where cne_etud like '"+cne+"'" ;
            ResultSet rq = st.executeQuery(SQL);
         rq.next();
            int idf = rq.getByte("filiere");
           
                PdfWriter.getInstance(doc , new FileOutputStream("print.pdf"));
                doc.open();
                SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
                Date now = new Date();
                String dtnow = dt.format(now);
                Paragraph pr = new Paragraph();
                Paragraph dat = new Paragraph();
                Paragraph title = new Paragraph("Relevé de Notes\n\n",FontFactory.getFont(FontFactory.TIMES,20,Font.BOLD,BaseColor.BLUE));
                dat.add(dtnow);
                title.setAlignment(Element.ALIGN_CENTER);
                dat.setAlignment(Element.ALIGN_RIGHT);
                doc.add(dat);
                doc.add(title);
                pr.add("nom et prenom: "+rq.getString("nom_etud")+" "+rq.getString("prenom_etud")+"\n");
               pr.add("Semestre: "+rq.getString("semestre")+" \n");
                 String filiere = "SELECT * FROM filiere where id_filiere = "+idf ;
            ResultSet rqf = st.executeQuery(filiere);
            rqf.next();
                pr.add("Filière: "+rqf.getString("intitule_filiere")+"\n\n");
                doc.add(pr);
            String note = "SELECT * FROM note where cneEtud = '"+cne+"'" ;
            ResultSet rqn = st.executeQuery(note);
                PdfPTable tb = new PdfPTable(2);
                tb.addCell("Module");
                tb.addCell("Note");
                
                 while(rqn.next()){
                int idm = rqn.getByte("modules");
                String nmodule = null;
                     Statement stm = connect.con.createStatement();
                     System.out.println(idm);
                    String module = "SELECT * FROM modules where id_module = "+idm ;
                    ResultSet rqm = stm.executeQuery(module);
                    while(rqm.next()){
                        nmodule = rqm.getString("module");
                }
                                  tb.addCell(nmodule);                 
                                    tb.addCell(rqn.getString("note"));
                 }
                doc.add(tb);
                doc.close();
                Desktop opn = Desktop.getDesktop();
                if(opn.isSupported(Desktop.Action.OPEN)){
                    opn.open(new File("print.pdf"));
                }else{
                    System.out.println("Open is Not Supported");
                }
            } catch (DocumentException | FileNotFoundException | ClassNotFoundException | SQLException ex) {
                Logger.getLogger(releveNote.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
            Logger.getLogger(releveNote.class.getName()).log(Level.SEVERE, null, ex);
        }
            

    }//GEN-LAST:event_jButton1MouseClicked

    private void jToggleButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jToggleButton1MouseClicked
        // TODO add your handling code here:
        model = (DefaultTableModel) jTable1.getModel();
        model.getDataVector().removeAllElements();
            try {
                 cne = jTextField1.getText();
            connection connect = new connection();
            Statement st = connect.con.createStatement();
            String etud = "SELECT * FROM etudiant where cne_etud like '"+cne+"'" ;
            ResultSet rq = st.executeQuery(etud);
            rq.next();
            jLabel2.setText("Nom : "+rq.getString("nom_etud"));
            jLabel3.setText("Prenom : "+rq.getString("prenom_etud"));  
            jLabel5.setText("Semestre : "+rq.getString("semestre"));
            int idf = rq.getByte("filiere");
            String filiere = "SELECT * FROM filiere where id_filiere = "+idf ;
            ResultSet rqf = st.executeQuery(filiere);
            rqf.next();
            jLabel4.setText("Filiere : "+rqf.getString("intitule_filiere"));
            String note = "SELECT * FROM note where cneEtud = '"+cne+"'" ;
            ResultSet rqn = st.executeQuery(note);
            while(rqn.next()){
                int idm = rqn.getByte("modules");
                String nmodule = null;
              
                     Statement stm = connect.con.createStatement();
                     System.out.println(idm);
                    String module = "SELECT * FROM modules where id_module = "+idm ;
                    ResultSet rqm = stm.executeQuery(module);
                    while(rqm.next()){
                        nmodule = rqm.getString("module");
                }
                                  model.addRow(new Object[]{nmodule,rqn.getString("note")});

                
            }

        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(releveNote.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jToggleButton1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(releveNote.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(releveNote.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(releveNote.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(releveNote.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new releveNote().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables
}
